package com.globallogic.javatrainings.sergiiivanov.controller;

import com.globallogic.javatrainings.sergiiivanov.model.MyArray;
import com.globallogic.javatrainings.sergiiivanov.model.Student;
import com.globallogic.javatrainings.sergiiivanov.model.University;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityFailure;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;
import com.globallogic.javatrainings.sergiiivanov.resources.ErrorCodes;
import com.globallogic.javatrainings.sergiiivanov.resources.Messages;
import com.globallogic.javatrainings.sergiiivanov.view.StudentsListPage;

class StudentsMenuController extends BasicController {
    private enum states {
        ASK_NEW_STUDENT_INFO, SHOW_LIST
    }

    private StudentsListPage page = new StudentsListPage();
    private int command;
    private states currentState = states.SHOW_LIST;
    private University university;

    @Override
    BasicController initiate(University u) throws UniversityFailure {
        this.university = u;
        validateData();
        if (currentState == states.SHOW_LIST) {
            return showStudentListMenu();
        }
        if (currentState == states.ASK_NEW_STUDENT_INFO) {
            return addStudent();
        }
        return this;
    }

    private BasicController addStudent() {

        page.pageAddStudentName();
        String newStudentName = InputController.readString().trim();
        page.pageAddStudentPhone();
        String newStudentPhone = InputController.readString().trim();
        page.pageAddStudentEmal();
        String newStudentEmail = InputController.readString().trim();
        try {
            university.students.addPerson(newStudentName, newStudentPhone, newStudentEmail);
        } catch (UniversityWarning e) {
            page.error(Messages.ERROR_FAILED_TO_ADD_NEW_STUDENT, e.getMessage());
        }
        currentState = states.SHOW_LIST;
        return this;
    }

    private BasicController showStudentListMenu() throws UniversityFailure {
        MyArray<String> menu = new MyArray<String>(new String[0]);
        menu.add(Messages.MENU_BACK);
        for (Student student : university.students.getStudents()) {
            menu.add(student.getName());
        }
        menu.add(Messages.ADD_NEW_STUDENT);
        page.pageStudentsList(menu.toArray());
        command = InputController.readInt();
        if (command == 0 || command >= menu.size()) {
            return null;
        }
        if (command == menu.size() - 1) {
            currentState = states.ASK_NEW_STUDENT_INFO;
            return this;
        }
        try {
            return new StudentDetailsController(university.students.getStudentByIndex(command - 1));
        } catch (UniversityWarning e) {
            page.error(Messages.FAILED_TO_READ_STUDENT, e.getMessage());
            return this;
        }
    }

    private void validateData() throws UniversityFailure {
        if (university == null || university.students == null) {
            throw new UniversityFailure(ErrorCodes.CONTROLLERS_STUDENTS_DATA_VALIDATION_FAILED);
        }
    }

}
